<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BPCategory
 *
 * @author i062261
 */
class BCategory extends BTreeEntity {

    //put your code here
    public $name;
    public $value;
    public $owner;
    public $ownerType;
    public $ownerId;
    public $valueType;

    public function __construct($item = NULL, $owner = NULL) {
        if ($owner == NULL) {
            $owner = Blyn::app();
        }
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->ownerType = get_class($owner);
            if ($owner->getId() != NULL)
                $this->ownerId = $owner->getId();
        }

        parent::__construct($item);
    }

    public function setOwner($owner) {
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->ownerType = get_class($owner);
            if ($owner->getId() != NULL)
                $this->ownerId = $owner->getId();
        }
    }

    public function getName($data = NULL) {
        return $this->name;
    }

    public function getValue($data = NULL) {
        if ($this->valueType == 'string' || $this->valueType == NULL) {
            return $this->value;
        }
        if ($this->valueType == 'non-string') {
            $data = $this->data;
            return unserialize($data);
        }
    }

    public function getRootNode($context = NULL) {
        $root = self::getRoot();

        $myContext = $this->getRootNodeContext($context);

        $myRoot = $root->getChild($myContext);

        if ($myRoot instanceof BTreeEntity && $myRoot->getId() != NULL) {
            return $myRoot;
        } else {
            $myRoot = $root->addChild($myContext);
            return $myRoot;
        }
    }

    public function getRootNodeContext($context = NULL) {
        $myContext = array(
            'name' => $this->ownerType . $this->ownerId . '_root',
            'spaceId' => Blyn::app()->getActiveSpace()->getId(),
            //'appId' => Blyn::app()->getActiveApp()->getId(),
            'ownerType' => $this->ownerType,
            'ownerId' => $this->ownerId,
                //'myClass' => get_class($this)
        );

        if (is_array($context)) {
            $myContext += $context;
        }

        return $myContext;
    }

    public function save($context = null) {
        if ($this->value != NULL) {
            if (is_string($this->value) || is_numeric($this->value)) {
                $this->valueType = 'string';
            } else {
                $this->data = serialize($this->value);
                $this->valueType = 'non-string';
            }
        }

        parent::save($context);
    }

    public function getCategoryFullName($seperator = '.', $data = null) {
        $fullName = parent::getFullName($seperator, $data);
        $list = explode($seperator, $fullName);

        $cName = str_replace($list[0] . '.', '', $fullName);

        return $cName;
    }

    public function getModelClass($data = NULL) {
        return 'BMCategory';
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->setModel($this->getModelClass());
        $repository->addPropertyToColumnMap('value', 'value');
        $repository->addPropertyToColumnMap('ownerType', 'owner_type');
        $repository->addPropertyToColumnMap('ownerId', 'owner_id');
        $repository->addPropertyToColumnMap('valueType', 'value_type');
        return $repository;
    }

    public static function getMemberCategory($member, $parentPrefix = NULL) {
        if ($member instanceof BEntity) {
            $memberClass = get_class($member);
            if ($member instanceof BTreeList) {
                $memberClass = get_class($member->getListNode());
            }
            if ($member->getId() != NULL) {
                //$categoryMemberTable = Blyn::app()->getActiveApp()->getSheet('BPCategoryMeta', 'categoryMember');
                $categoryMemberTable = new BTreeTable('BPCategoryMeta', Blyn::app()->getActiveApp(), 'categoryMember');
                $view = $categoryMemberTable->getView(array('itemData' => array('memberId' => $member->getId(), 'memberClass' => $memberClass)));
                foreach ($view as $list) {
                    $categoryName = $list->getItem('categoryName')->getValue();
                    $categoryId = $list->getItem('categoryId')->getValue();
                    if (is_string($parentPrefix)) {
                        if (strpos($categoryName, $parentPrefix) !== FALSE) {
                            if (is_numeric($categoryId)) {
                                return new BPCategory($categoryId);
                            }
                        }
                    } else {
                        if (is_numeric($categoryId)) {
                            return new BPCategory($categoryId);
                        }
                    }
                }
            }
        }
    }

    public function getMembers($memberContext = null, $recursive = FALSE) {
        $listContext = array();
        $listContext['categoryId'] = $this->getId();
        $memberClass = NULL;
        if (is_string($memberContext)) {
            $memberClass = $memberContext;
        }
        if ($memberContext instanceof BEntity) {
            $memberClass = get_class($memberContext);
        }
        if (is_array($memberContext)) {
            $memberClass = $memberContext['memberClass'];
        }

        if ($memberContext != NULL) {
            $categoryMemberTable = Blyn::app()->getActiveApp()->getSheet('BPCategoryMeta', 'categoryMember');
            $view = $categoryMemberTable->getView(array('itemData' => $listContext));
            $memberList = array();
            foreach ($view as $list) {
                $memberClass = $list->getItem('memberClass')->getValue();
                $memberId = $list->getItem('memberId')->getValue();
                if ($memberContext instanceof BTreeList) {
                    $metaClass = $memberClass;
                    $treeListNode = new $metaClass($memberId);
                    if($treeListNode->status == 0){
                    $treeList = new BTreeList($treeListNode);
                    $memberList[$memberId] = $treeList;
                    }
                } else if (class_exists($memberClass)) {
                    $member = new $memberClass($memberId);
                    $memberList[$memberId] = $member;
                }
            }
            return $memberList;
        }
    }

    public function addMember($member, $member2 = NULL, $context = NULL) {
        if ($member instanceof BEntity && $member->getId() != NULL) {
            $memberId = $member->getId();
            $memberClass = get_class($member);
            if ($member instanceof BTreeList) {
                $listNode = $member->getListNode();
                $memberClass = get_class($listNode);
            }
            $categoryMemberTable = new BTreeTable('BPCategoryMeta', Blyn::app()->getActiveApp(), 'categoryMember');
            //$categoryMemberTable = Blyn::app()->getActiveApp()->getSheet('BPCategoryMeta', 'categoryMember');
            $list = array(
                'categoryId' => $this->getId(),
                'memberId' => $memberId,
                'memberClass' => $memberClass,
                'categoryName' => $this->getFullName());
            $categoryMemberTable->addList($list);
        }
    }

}
